home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / native1.1 / implementing / example-1.1 / attach.c next >
Encoding:
C/C++ Source or Header  |  1997-07-13  |  2.9 KB  |  102 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /* Note: This program only works on Win32.
  18. */
  19. #include <windows.h>
  20. #include <jni.h>
  21.  
  22. JavaVM *jvm;
  23.  
  24. void thread_fun(void *arg)
  25. {
  26.     jint res;
  27.     jclass cls;
  28.     jmethodID mid;
  29.     jstring jstr;
  30.     jobjectArray args;
  31.     JNIEnv *env;
  32.     char buf[100];
  33.     int threadNum = (int)arg;
  34.  
  35.     JDK1_1AttachArgs targs;
  36.  
  37.     res = (*jvm)->AttachCurrentThread(jvm, &env, &targs);
  38.     if (res < 0) {
  39.        fprintf(stderr, "Thread %d: attach failed\n", threadNum);
  40.        return;
  41.     }
  42.  
  43.     cls = (*env)->FindClass(env, "Prog");
  44.     if (cls == 0) {
  45.         fprintf(stderr, "Thread %d: Can't find Prog class\n", threadNum);
  46.         return;
  47.     }
  48.  
  49.     mid = (*env)->GetStaticMethodID(env, cls, "main", "([Ljava/lang/String;)V");
  50.     if (mid == 0) {
  51.         fprintf(stderr, "Thread %d: Can't find Prog.main\n", threadNum);
  52.     return;
  53.     }
  54.  
  55.     sprintf(buf, " from Thread %d", threadNum);
  56.     jstr = (*env)->NewStringUTF(env, buf);
  57.     if (jstr == 0) {
  58.         fprintf(stderr, "Thread %d: Out of memory\n", threadNum);
  59.         return;
  60.     }
  61.     args = (*env)->NewObjectArray(env, 1,
  62.                         (*env)->FindClass(env, "java/lang/String"), jstr);
  63.     if (args == 0) {
  64.         fprintf(stderr, "Thread %d: Out of memory\n", threadNum);
  65.         return;
  66.     }
  67.     args = (*env)->NewObjectArray(env, 1,
  68.                         (*env)->FindClass(env, "java/lang/String"), jstr);
  69.     if (args == 0) {
  70.         fprintf(stderr, "Thread %d: Out of memory\n", threadNum);
  71.         return;
  72.     }
  73.     (*env)->CallStaticVoidMethod(env, cls, mid, args);
  74.     (*jvm)->DetachCurrentThread(jvm);
  75. }
  76.  
  77. main() {
  78.     JNIEnv *env;
  79.     JDK1_1InitArgs vm_args;
  80.     int i;
  81.     jint res;
  82.  
  83.     /* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
  84.     vm_args.version = 0x00010001;
  85.  
  86.     JNI_GetDefaultJavaVMInitArgs(&vm_args);
  87.     res = JNI_CreateJavaVM(&jvm, &env, &vm_args);
  88.     if (res < 0) {
  89.         fprintf(stderr, "Can't create Java VM\n");
  90.         exit(1);
  91.     }
  92.  
  93.     for (i=0; i<5; i++)
  94.         /* We pass the thread number as the argument to every thread */
  95.         _beginthread(thread_fun,0,(void *)i);
  96.  
  97.     Sleep(5000); /* wait for threads to finish */
  98.  
  99.     (*jvm)->DestroyJavaVM(jvm);
  100. }
  101.  
  102.